<?php
// +----------------------------------------------------------------------
// | A3Mall
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: xzncit <158373108@qq.com>
// +----------------------------------------------------------------------

namespace app\common\model\users;

use app\common\model\base\A3Mall;

class WithdrawLog extends A3Mall{
    protected $autoWriteTimestamp = true;
    protected $name = "withdraw_log";

    public const CAHNNEL_ALIPAY=1;
    public const CAHNNEL_WXPAY=2;

    protected $type = [
        "id"=>"integer",
        "user_id"=>"integer",
        "amount"=>"float",
        "channel"=>"integer",
        "payee_account"=>"string",
        "create_time"=>"integer",
    ];

    //待审核
    public const STATUS_WAIT=1;
    //成功
    public const STATUS_SUCCESS=2;
    //失败
    public const STATUS_FAILED=3;

    protected static $mapping=[
        self::CAHNNEL_ALIPAY=>'支付宝',
        self::CAHNNEL_WXPAY=>'微信',
    ];

    protected static $status_mapping=[
        self::STATUS_WAIT=>'待审核',
        self::STATUS_SUCCESS=>'成功',
        self::STATUS_FAILED=>'失败',
    ];

    public static function getChannelList()
    {
        return self::$mapping;
    }

    public static function getChannelText(int $channel)
    {
        return self::$mapping[$channel] ?? '';
    }

    //获取状态文本
    public static function getStatusText(int $status){
        return self::$status_mapping[$status] ?? '';
    }

    //判断状态是否有效
    public static function isValidStatus(int $status)
    {
        return array_key_exists($status,static::getValidStatusList());
    }

    //获取状态列表
    public static function getStatusList()
    {
        return self::$status_mapping;
    }

    public static function getValidStatusList()
    {
        $ret=self::$status_mapping;
        unset($ret[static::STATUS_WAIT]);
        return $ret;
    }

    public function users(){
        return $this->hasOne(Users::class,"id","user_id")
            ->bind(["username"])
            ->joinType("INNER");
    }

    public function get_list($condition=[],$size=10,$page=1){
        // echo $this->withJoin("users")->where($condition)->fetchSql(true)->select();exit();
        $count = $this->withJoin("users")->where($condition)->count();
        $data = $this->withJoin("users")->where($condition)->order('withdraw_log.id','desc')->paginate($size);
        return [
            "count"=>$count,
            "data"=>array_map([$this,'format'],$data->items())
        ];
    }

//    public function setBankNameAttr($value){
//        return strip_tags(trim($value));
//    }
//
//    public function setBankRealName($value){
//        return strip_tags(trim($value));
//    }
//
//    public function setCodeAttr($value){
//        return strip_tags(trim($value));
//    }
//
//    public function setAddressAttr($value){
//        return strip_tags(trim($value));
//    }

    public function setAccountAttr($value){
        return strip_tags(trim($value));
    }

//    public function setMarkAttr($value){
//        return strip_tags(trim($value));
//    }
//
//    public function setMsgAttr($value){
//        return strip_tags(trim($value));
//    }

   public function getCreateTimeAttr($value){
       return date("Y-m-d H:i:s",$value);
   }
//
//   public function getUpdateTimeAttr($value){
//       return date("Y-m-d H:i:s",$value);
//   }

    public function getAmountAttr($value){
        return sprintf('%.2f',$value);
    }

    public function getChannelAttr($value){
        return self::$mapping[$value] ?? '';
    }

    public function getChannelTextAttr($value,$data)
    {
        return self::$mapping[$data['channel']] ?? '';
    }

    public function getStatusTextAttr($value,$data)
    {
        return self::$status_mapping[$data['status']] ?? '';
    }

    protected function format($value)
    {
        $value->channel_name=$value->channel_text;
        $value->status_text=$value->status_text;
        return $value;
    }
}
